package com.yanqu.road.server.cmd.children;

import com.yanqu.road.entity.config.children.ChildTalentInfo;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.entity.player.UserChildSite;
import com.yanqu.road.pb.child.ChildProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.ChildModule;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.config.ChildMgr;
import com.yanqu.road.server.manger.marriage.MarriageMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.command.Cmd;

import java.util.Objects;

@Cmd(code = Protocol.S_CHILD_ADULT, desc = "子女成年")
public class ChildAdultCmd extends AbstractCommand {

    private int siteIndex;

    private String childUid;

    private ChildProto.ChildAdultRespMsg.Builder respMsg;


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ChildProto.ChildAdultReqMsg req = ChildProto.ChildAdultReqMsg.parseFrom(packet.getBytes());
        siteIndex = req.getSiteIndex();
        childUid = req.getChildUid();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        respMsg  = ChildProto.ChildAdultRespMsg.newBuilder();

        ChildModule childModule = player.getModule(ChildModule.class);
        UserChildSite userChildSite = childModule.getUserChildSite(siteIndex);
        if (null == userChildSite) {//不存在该子女
            ret = GameErrorCode.E_CHILD_SITE_NO_FOUND;
            return;
        }

        if(!Objects.equals(userChildSite.getChildUid(), childUid)){
            ret = GameErrorCode.E_CHILD_SITE_HAS_CHANGE;
            return;
        }

        // 是否已经培养完成
        ChildTalentInfo childTalent = ChildMgr.getChildTalentInfo(userChildSite.getChildTalent());
        if (userChildSite.getCurrentGrowth() < childTalent.getTotalGrowth()) {
            ret = GameErrorCode.E_CHILD_TRAIN_NO_COMPLETE;
            return;
        }

        //是否有足够席位
        int unMarryCount = MarriageMgr.getUnMarryChildCount(player.getUserId());
        int maxChildrenMarriageSite = GameConfig.CHILDREN_MARRIAGE_SITE;
        maxChildrenMarriageSite += CurioMgr.getSkillAdditionBySkillType(eSkillType.ChildUnMarriageLimitSkill.getValue(), player, 0);
        if (unMarryCount >= maxChildrenMarriageSite) {
            ret = GameErrorCode.E_MARRIAGE_SITE_NOT_ENOUGH;
            return;
        }

        UserChildSite cloneSite = userChildSite.clone();     //克隆一份出来用

        //先清除席位信息
        childModule.clearChildSite(userChildSite);

        //生双胞胎
        if(cloneSite.getChildType() != 0){
            ChildProto.ChildAdultTempMsg.Builder adultChild = childModule.adultChild(cloneSite, 1);
            respMsg.addChildren(adultChild);
            ChildProto.ChildAdultTempMsg.Builder adultChild2 = childModule.adultChild(cloneSite, 2);
            respMsg.addChildren(adultChild2);
        }else{
            ChildProto.ChildAdultTempMsg.Builder adultChild = childModule.adultChild(cloneSite, 0);
            respMsg.addChildren(adultChild);
        }


        childModule.syncChildSiteData(userChildSite);
        //成就、任务
        player.notifyListener(eGamePlayerEventType.ChildTotalAdultCount.getValue(), player.getModule(AttributeModule.class).getAttribute(ePlayerAttrType.ChildTotalAdultCount));
    }




    @Override
    public void sendData(GamePlayer player) {
        respMsg.setRet(ret);
        player.sendPacket(Protocol.U_CHILD_ADULT, respMsg);
    }
}
